package org.dyndns.opendemogroup.optimizer.selections;

import static org.junit.Assert.*;

import java.util.Random;

import org.junit.Test;

/**
 * A class to test the class RandomElite
 */
public class RandomEliteTest
{

	/**
	 * Tests the <i>choose</i> method with a simulation that checks that only
	 * the portion identified is selected.
	 */
	@Test
	public void choose_Simulation ( )
	{
		final int popSize = 200;
		final int max = 30;
		int[] indices = new int[popSize];
		runSimulation ( popSize, max, indices, false, 0, max, max, popSize );
		runSimulation ( popSize, max, indices, true, popSize - max, popSize, 0,
			popSize - max );
	}

	private void runSimulation ( final int popSize, final int max,
			int[] indices, boolean isMaximizing, int gzLow, int gzHigh,
			int ezLow, int ezHigh )
	{
		Random randomSource = new Random ( 42 );
		for ( int i = 0; i < popSize; i++ )
		{
			indices[i] = 0;
		}
		for ( int i = 0; i < 10000; i++ )
		{
			int actual =
				RandomElite.choose ( popSize, randomSource, max, isMaximizing );
			indices[actual]++;
		}
		for ( int i = gzLow; i < gzHigh; i++ )
		{
			assertTrue ( "Greater than zero, maximizing = " + isMaximizing,
				indices[i] > 0 );
		}
		for ( int i = ezLow; i < ezHigh; i++ )
		{
			assertTrue ( "Equal to zero, maximizing = " + isMaximizing,
				0 == indices[i] );
		}
	}
}
